package com.ruoyi.system.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.WlwsysDevice;
import com.ruoyi.common.utils.random.RandomUtil;
import com.ruoyi.system.mapper.wlwsys.WlwsysDeviceMapper;
import com.ruoyi.system.service.WlwsysDeviceService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86187
* @description 针对表【wlwsys_device】的数据库操作Service实现
* @createDate 2023-07-19 17:34:42
*/
@Service
public class WlwsysDeviceServiceImpl extends ServiceImpl<WlwsysDeviceMapper, WlwsysDevice>
    implements WlwsysDeviceService {

    @Autowired
    private WlwsysDeviceMapper wlwsysDeviceMapper;

    @Autowired
    private WlwsysProjectServiceImpl wlwsysProjectService;

    //条件分页查询
    @Override
    public List<WlwsysDevice> getDeviceByPageInfo(Integer pageNum, Integer pageSize, String deviceId, String deviceName, String projectId, String startTime,String endTime) throws ParseException {
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isBlank(deviceId)) {
            deviceId="";
        }
        if (StringUtils.isBlank(deviceName) ) {
            deviceName="";
        }
        if (StringUtils.isBlank(projectId) ) {
            projectId="";
        }
        if(StringUtils.isBlank(startTime)){
            return wlwsysDeviceMapper.selectDevice(deviceId,deviceName,projectId);
        }
        startTime = startTime.replace("Z", " UTC");
        endTime = endTime.replace("Z", " UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat chinaSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = chinaSdf.format(sdf.parse(startTime));
        endTime = chinaSdf.format(sdf.parse(endTime));
        return wlwsysDeviceMapper.selectDeviceHaveTime(deviceId,deviceName,projectId,startTime,endTime);
    }

    //删除数据通过设备ID
    @Override
    public void deleteByDeviceId(String deviceId) {
        wlwsysDeviceMapper.deleteByDeviceId(deviceId);
    }

    //新增设备
    @Override
    public void insertDevice(String deviceName, String deviceIntroduce, String projectId) throws ParseException {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date);
        String deviceId = "d_" + RandomUtil.getRandomNumber(8);
        if(deviceName==null){
            deviceName="";
        }
        if(deviceIntroduce==null){
            deviceIntroduce="";
        }
        if(projectId==null){
            projectId="";
        }
        wlwsysDeviceMapper.insertNew(deviceId,deviceName,deviceIntroduce,projectId,time);
    }

    //更新设备
    @Override
    public boolean UpdateDevice(String deviceId, String deviceName, String deviceIntroduce, String projectId) throws ParseException {
        //获取当前的时间
        Date date1 = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date1);
        if(deviceName==null){
            deviceName="";
        }
        if(deviceIntroduce==null){
            deviceIntroduce="";
        }
        if(projectId==null){
            projectId="";
        }
        return wlwsysDeviceMapper.updateByDeviceId(deviceId,deviceName,deviceIntroduce,projectId,time);
    }

    @Override
    public List<String> selectDropdownForAddDevice(Long userId) throws ParseException {
        return wlwsysProjectService.selectProjectIdByUserId(userId);
    }

    @Override
    public List<String> selectDropdownForUpdateDevice(String projectId) throws ParseException {
        return wlwsysProjectService.selectProjectIdByUserId(wlwsysProjectService.selectUserIdByProjectId(projectId));
    }
}




